<template>
  <div>
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="部门编号" prop="code">
        <el-input v-model="form.code" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" placeholder="请选择负责人">
          <el-option v-for="item in employees" :key="item.id" :label="item.username" :value="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="活动形式" prop="introduce">
        <el-input v-model="form.introduce" type="textarea" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="deptSubmit">确定</el-button>
        <el-button @click="cannel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getEmployeeSimple, addDepartments, getDepartDetail, updateDepartments } from '@/api/departments'
export default {
  name: 'DeptDialog',
  props: {
    parentId: {
      type: [String, Number],
      required: true
    },
    isEditor: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      form: {
        name: '',
        code: '',
        introduce: '',
        manager: ''
      },
      employees: [],
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getEmployeeSimple()
    // console.log(this.parentId)
    this.getDepartDetail()
  },
  methods: {
    async deptSubmit() {
      this.$refs.form.validate(valid => {
        if (!valid) return
        this.isEditor === true ? this.updateDepartments() : this.addDepartments()
      })
    },
    // 添加部门
    async addDepartments() {
      const res = await addDepartments({
        ...this.form,
        pid: this.parentId
      })
      this.$message.success(res.message)
      this.$emit('updataList')
    },

    // 更新部门
    async updateDepartments() {
      const res = await updateDepartments(this.form)
      console.log(res)
      this.$emit('upDate')
    },

    // 获取部门详情
    async getDepartDetail() {
      if (this.isEditor) {
        const res = await getDepartDetail(this.parentId)
        console.log(res)
        console.log(11111)
        console.log(this.isEditor)
        this.form = res.data
      }
    },
    cannel() {
      this.$emit('cannel')
    },
    // 获取员工列表
    async getEmployeeSimple() {
      const res = await getEmployeeSimple()
      // console.log(res)
      this.employees = res.data
    }

  }

}
</script>

<style lang="scss" scoped>
 form.el-form{
     padding: 0 60px 0 20px;
 }
 .el-select {
     width: 100%;
 }

</style>
